from typing import List

from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession

from server.config.session import with_session
from server.module_chat.chat.entity.vo.conversation import Conversation
from server.module_chat.chat.entity.vo.message import Message


class ChatDao:
    @classmethod
    @with_session
    async def get_conversation_list(cls, session: AsyncSession, user_id: str) -> List[Conversation]:
        """
        获取会话列表
        :param session:
        :param user_id:
        :return:
        """
        stmt = select(Conversation).where(Conversation.user_id == user_id)
        result = await session.execute(stmt)
        return result.scalars().all()

    @classmethod
    @with_session
    async def add_conversation(cls, session: AsyncSession, conversation: Conversation) -> Conversation:
        """
        添加会话
        :param session:
        :param conversation: 会话对象
        :return:
        """
        session.add(conversation)
        return conversation

    @classmethod
    @with_session
    async def get_message_list(cls, session: AsyncSession, conversation_id: str) -> List[Message]:
        """
        获取会话列表
        :param session:
        :param conversation_id: 会话id
        :return:
        """
        stmt = select(Message).where(Message.conversation_id == conversation_id)
        result = await session.execute(stmt)
        return result.scalars().all()

    @classmethod
    @with_session
    async def add_message(cls, session: AsyncSession, message: Message) -> Message:
        """
        添加会话
        :param session:
        :param message: 会话对象
        :return:
        """
        session.add(message)
        return message


